package edu.mse.camel.client.playback.actions;

import org.eclipse.core.runtime.IStatus;
import org.eclipse.core.runtime.Status;
import org.eclipse.jface.action.IAction;
import org.eclipse.jface.dialogs.ErrorDialog;
import org.eclipse.jface.viewers.ISelection;
import org.eclipse.swt.widgets.Shell;
import org.eclipse.ui.IObjectActionDelegate;
import org.eclipse.ui.IViewActionDelegate;
import org.eclipse.ui.IViewPart;
import org.eclipse.ui.IWorkbenchPage;
import org.eclipse.ui.IWorkbenchPart;
import org.eclipse.ui.PartInitException;
import org.eclipse.ui.PlatformUI;

import edu.mse.camel.client.eventbus.pubsub.EventManager;
import edu.mse.camel.client.events.server.JoinSessionEvent;
import edu.mse.camel.client.session.team.TEAMMEMBER_STATUS;
import edu.mse.camel.client.session.team.TeamList;
import edu.mse.camel.client.session.team.TeamVewConstants;
import edu.mse.camel.client.playback.Activator;
import edu.mse.camel.client.playback.dialog.PlaybackDialog;
import edu.mse.camel.client.playback.views.PlaybackMessageList;

public class PlaybackActionDelegate implements IObjectActionDelegate,
IViewActionDelegate {
	
	private String title;

	@Override
	public void setActivePart(IAction action, IWorkbenchPart targetPart) {
		// TODO Auto-generated method stub
		
	}

	@Override
	public void run(IAction action) {
		if (!TeamList.getInstance().getMyStatus().equals(TEAMMEMBER_STATUS.OFFLINE)) {
			if (!TeamList.getInstance().getMyStatus().equals(TEAMMEMBER_STATUS.SESSION)) {
				playback();
			} else {
				ErrorDialog ed = new ErrorDialog(new Shell(),
						"Error",
						"You are already in session. Please leave session first", 
						new Status(
								IStatus.ERROR, Activator.PLUGIN_ID,
								"You are already in session. Please leave session first"),
						IStatus.ERROR);
				ed.open();				
			}
		} else {
			ErrorDialog ed = new ErrorDialog(new Shell(),
					"Error",
					"Please login first.", new Status(
							IStatus.ERROR, Activator.PLUGIN_ID,
							"Please login first."),
					IStatus.ERROR);
			ed.open();
		}

	}

	@Override
	public void selectionChanged(IAction action, ISelection selection) {
		// TODO Auto-generated method stub	
		
	}

	@Override
	public void init(IViewPart view) {
		// TODO Auto-generated method stub
		
	}
	
	public void playback() {
		Shell shell = new Shell();
		PlaybackDialog playbackDialog = new PlaybackDialog(shell);
		playbackDialog.open();

		title = playbackDialog.getSessionName();
		float speed = playbackDialog.getSpeed();

		if (title.length() > 0) {

			JoinSessionEvent jse = new JoinSessionEvent(getSessionID(title));
			jse.setPlayback(true);
			EventManager.getInstance().dispatchEventSynchronous(jse, true);
			
			if (jse.isOpSuccessful()) {
				TeamList.getInstance().setPlayback(true);
				PlaybackMessageList.getInstance().setSpeed(speed);
				IWorkbenchPage page = PlatformUI.getWorkbench().getActiveWorkbenchWindow().getActivePage();
				IViewPart view = page.findView("edu.mse.camel.client.chat.views.ChatView");
				if(view != null)
					page.hideView(view);
		
				try {
					page.showView("edu.mse.camel.client.playback.views.PlaybackView");
				} catch (PartInitException e) {
					// TODO Auto-generated catch block
					e.printStackTrace();
				}
			}
		}

	}
	
	private Long getSessionID(String sessionInfo) {
		return Long.valueOf(sessionInfo.substring(0, sessionInfo
				.indexOf(TeamVewConstants.SESSION_ID_TITLE_SEPARATOR)));
	}

}
